/*
 *
 */
package com.ePadink;


import java.awt.AWTException;
import java.awt.Image;
import java.awt.image.PixelGrabber;

class JpegInfo
{
  String Comment;
  public Image imageobj;
  public int imageHeight;
  public int imageWidth;
  public int[] BlockWidth;
  public int[] BlockHeight;
  public int Precision = 8;
  public int NumberOfComponents = 3;
  public Object[] Components;
  public int[] CompID = { 1, 2, 3 };
  public int[] HsampFactor = { 1, 1, 1 };
  public int[] VsampFactor = { 1, 1, 1 };
  public int[] QtableNumber = { 0, 1, 1 };
  public int[] DCtableNumber = { 0, 1, 1 };
  public int[] ACtableNumber = { 0, 1, 1 };
  public boolean[] lastColumnIsDummy = { false, false, false };
  public boolean[] lastRowIsDummy = { false, false, false };
  public int Ss = 0;
  public int Se = 63;
  public int Ah = 0;
  public int Al = 0;
  public int[] compWidth;
  public int[] compHeight;
  public int MaxHsampFactor;
  public int MaxVsampFactor;

  public JpegInfo(Image image)
  {
    this.Components = new Object[this.NumberOfComponents];
    this.compWidth = new int[this.NumberOfComponents];
    this.compHeight = new int[this.NumberOfComponents];
    this.BlockWidth = new int[this.NumberOfComponents];
    this.BlockHeight = new int[this.NumberOfComponents];
    this.imageobj = image;
    this.imageWidth = image.getWidth(null);
    this.imageHeight = image.getHeight(null);
    this.Comment = "JPEG Encoder Copyright 1998, James R. Weeks and BioElectroMech.  ";
    getYCCArray();
  }

  public void setComment(String comment) {
    this.Comment.concat(comment);
  }

  public String getComment() {
    return this.Comment;
  }

  private void getYCCArray()
  {
    int[] values = new int[this.imageWidth * this.imageHeight];

    PixelGrabber grabber = new PixelGrabber(this.imageobj.getSource(), 0, 0, this.imageWidth, this.imageHeight, values, 0, this.imageWidth);
    this.MaxHsampFactor = 1;
    this.MaxVsampFactor = 1;
    for (int y = 0; y < this.NumberOfComponents; y++) {
      this.MaxHsampFactor = Math.max(this.MaxHsampFactor, this.HsampFactor[y]);
      this.MaxVsampFactor = Math.max(this.MaxVsampFactor, this.VsampFactor[y]);
    }
    for (int y = 0; y < this.NumberOfComponents; y++) {
      this.compWidth[y] = ((this.imageWidth % 8 != 0 ? (int)Math.ceil(this.imageWidth / 8.0D) * 8 : this.imageWidth) / this.MaxHsampFactor * this.HsampFactor[y]);
      if (this.compWidth[y] != this.imageWidth / this.MaxHsampFactor * this.HsampFactor[y]) {
        this.lastColumnIsDummy[y] = true;
      }

      this.BlockWidth[y] = ((int)Math.ceil(this.compWidth[y] / 8.0D));
      this.compHeight[y] = ((this.imageHeight % 8 != 0 ? (int)Math.ceil(this.imageHeight / 8.0D) * 8 : this.imageHeight) / this.MaxVsampFactor * this.VsampFactor[y]);
      if (this.compHeight[y] != this.imageHeight / this.MaxVsampFactor * this.VsampFactor[y]) {
        this.lastRowIsDummy[y] = true;
      }
      this.BlockHeight[y] = ((int)Math.ceil(this.compHeight[y] / 8.0D));
    }
    try
    {
      if (grabber.grabPixels() != true)
      {
        try
        {
          throw new AWTException("Grabber returned false: " + grabber.status());
        } catch (Exception e) {
        }
      }
    } catch (InterruptedException e) {
    }
    float[][] Y = new float[this.compHeight[0]][this.compWidth[0]];
    float[][] Cr1 = new float[this.compHeight[0]][this.compWidth[0]];
    float[][] Cb1 = new float[this.compHeight[0]][this.compWidth[0]];
    float[][] Cb2 = new float[this.compHeight[1]][this.compWidth[1]];
    float[][] Cr2 = new float[this.compHeight[2]][this.compWidth[2]];
    int index = 0;
    for (int y = 0; y < this.imageHeight; y++)
    {
      for (int x = 0; x < this.imageWidth; x++)
      {
        int r = values[index] >> 16 & 0xFF;
        int g = values[index] >> 8 & 0xFF;
        int b = values[index] & 0xFF;

        Y[y][x] = ((float)(0.299D * r + 0.587D * g + 0.114D * b));
        Cb1[y][x] = (128.0F + (float)(-0.16874D * r - 0.33126D * g + 0.5D * b));
        Cr1[y][x] = (128.0F + (float)(0.5D * r - 0.41869D * g - 0.08130999999999999D * b));
        index++;
      }

    }

    this.Components[0] = Y;

    this.Components[1] = Cb1;

    this.Components[2] = Cr1;
  }

  float[][] DownSample(float[][] C, int comp)
  {
    int inrow = 0;
    int incol = 0;
    float[][] output = new float[this.compHeight[comp]][this.compWidth[comp]];
    for (int outrow = 0; outrow < this.compHeight[comp]; outrow++) {
      int bias = 1;
      for (int outcol = 0; outcol < this.compWidth[comp]; outcol++) {
        output[outrow][outcol] = ((C[inrow][(incol++)] + C[(inrow++)][(incol--)] + C[inrow][(incol++)] + C[(inrow--)][(incol++)] + bias) / 4.0F);
        bias ^= 3;
      }
      inrow += 2;
      incol = 0;
    }
    return output;
  }
}